import Vue from 'vue'
import Router from 'vue-router'
import index from '@/components/index'
import myindex from '@/components/myindex'
import myregister from '@/components/myall/myregister'
import mylogin from '@/components/myall/mylogin'
import mycenter from '@/components/myall/mycenter'
import myhome from '@/components/myall/myhome'
import authorization from '@/components/myall/authorization'
import showcate from '@/components/myall/showcate'

Vue.use(Router)

var routes = [
  {
    path: '/',
    name: 'index',
    component: index
  },
  {
    path: '/myindex',
    name: 'myindex',
    component: myindex,
    beforeEnter: (to, from, next) => {
      // to去哪 from来自哪 next下一步执行
      if (localStorage.getItem("token")){
        console.log("已经登录过")
        next()
      }
      else{
        // next执行跳转
        alert("请先登录")
        next("/mylogin")
      }
    }
  },
  {
    path: '/myregister',
    name: 'myregister',
    component: myregister
  },
  {
    path: '/mylogin',
    name: 'mylogin',
    component: mylogin
  },
  {
    path: '/mycenter',
    name: 'mycenter',
    component: mycenter,
    beforeEnter: (to, from, next) => {
      // to去哪 from来自哪 next下一步执行
      if (localStorage.getItem("token")){
        console.log("已经登录过")
        next()
      }
      else{
        // next执行跳转
        alert("请先登录")
        next("/mylogin")
      }
    }
  },
  {
    path: '/myhome',
    name: 'myhome',
    component: myhome,
    beforeEnter: (to, from, next) => {
      // to去哪 from来自哪 next下一步执行
      if (localStorage.getItem("token")){
        console.log("已经登录过")
        next()
      }
      else{
        // next执行跳转
        alert("请先登录")
        next("/mylogin")
      }
    }
  },
  {
    path: '/authorization',
    name: 'authorization',
    component: authorization
  },
  {
    path: '/showcate',
    name: 'showcate',
    component: showcate
  },

]

export default new Router({
  routes: routes,
  mode: 'history'   /*hash*/
})
